/*
============================================================================
 Name        : StreamWriter.h
 Author      : 
 Version     :
 Copyright   : Your copyright notice
 Description : CStreamWriter declaration
============================================================================
*/

#ifndef __STREAMWRITER_H__
#define __STREAMWRITER_H__

// INCLUDES
#include <e32std.h>
#include <e32base.h>

#include <MdaAudioOutputStream.h> //CMdaAudioOutputStream, MMdaAudioOutputStreamCallback
#include <Mda\Common\Audio.h> //TMdaAudioDataSettings 
#include <mmf\common\mmfutilities.h> //TFourCC

#include "MStreamBufferObserver.h"	//MStreamBufferObserver

// CLASS DECLARATION

class CStreamBuffer;

class CStreamWriter : public CActive, public MMdaAudioOutputStreamCallback
{
public: // Constructors and destructor
	IMPORT_C static CStreamWriter* NewL(MStreamBufferObserver& aObserver);
	IMPORT_C static CStreamWriter* NewLC(MStreamBufferObserver& aObserver);


protected: // Constructors and destructor
	IMPORT_C CStreamWriter(MStreamBufferObserver& aObserver);
	IMPORT_C ~CStreamWriter();
	IMPORT_C virtual void ConstructL();


protected: //from CActive
	IMPORT_C void RunL();
	IMPORT_C void DoCancel();


protected: //from MMdaAudioOutputStreamCallback
	IMPORT_C void MaoscOpenComplete(TInt aError);
	IMPORT_C void MaoscBufferCopied(TInt aError, const TDesC8& aBuffer);
	IMPORT_C void MaoscPlayComplete(TInt aError);


public: //new functions
	IMPORT_C void StartL();

	IMPORT_C TInt Volume();
	IMPORT_C void SetVolume(TInt aVolume);


private: //new functions
	void ReadToBuffer();
	void Finished();

	void CloseAndClean();
	void SetBufferingState(TBool aIsBuffering, TInt aAvailable);

private:
	void SetStreamBuffer(CStreamBuffer* aStreamBuffer);
	friend class CStreamPlayer;


protected: //data
	//Object state
	enum TState
	{
		ENotReady,
		EBuffering,
		EFinished,
		EFailed
	} iState;

	//Observer
	MStreamBufferObserver& iObserver;

	//Audio settings
	CMdaAudioOutputStream* iStreamOutput;
	TMdaAudioDataSettings iSettings;
	TMdaAudioDataSettings iAudioSettings;
	TFourCC iDataType;

	//Stream buffer
	CStreamBuffer* iStreamBuffer;

	//Check if is writing
	TBool iWritingToStream;

	//Buffering
	TBool iIsBuffering;
	TInt iAvailableBytes;

	//Volume
	TInt iVolumeStep;
	TInt iVolume;

	//Timer who provides async timing service
	RTimer	iTimer;		

	//Buffer to hold read
	TUint8* iBufferToRead;
	TPtr8 iBufferToReadPtr;
};

#endif // __STREAMWRITER_H__
